﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;

namespace FastDeal.Admin
{
    public partial class QuanLiHoaDon : System.Web.UI.Page
    {
        private HoaDon_BUS hd_Bus = new HoaDon_BUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DaDangNhap"] == null || (bool)Session["DaDangNhap"] == false)
            {
                Response.Redirect("~/Default.aspx");
            }
            if ((int)Session["LoaiThanhVien"] != 1)
            {
                Response.Redirect("~/Default.aspx");
            }

            List<HoaDon> dsHoaDon = new List<HoaDon>();
            dsHoaDon = hd_Bus.LayDanhSach_HoaDon();
            GV_DSHoaDon.DataSource = dsHoaDon;
            GV_DSHoaDon.DataBind();
            for(int i=0;i<dsHoaDon.Count-1;i++)
            {
                if (dsHoaDon[i].TinhTrang==0)
                {
                    GV_DSHoaDon.Rows[i].Cells[1].BackColor = Color.Goldenrod;
                    GV_DSHoaDon.Rows[i].Cells[2].BackColor = Color.Goldenrod;
                    GV_DSHoaDon.Rows[i].Cells[3].BackColor = Color.Goldenrod;
                    GV_DSHoaDon.Rows[i].Cells[4].BackColor = Color.Goldenrod;
                    GV_DSHoaDon.Rows[i].Cells[5].BackColor = Color.Goldenrod;
                }
            }
            if (Panel1.Height.Value <= 800)
            {
                Panel1.Height = 1000;
            }
            else
            {
                Panel1.Height = 1200;
            }
            double  tongTien = 0;
            foreach (HoaDon hd in dsHoaDon)
            {
                tongTien = tongTien + hd.TongTien;
            }
            lb_tongDoanhThu.Text = tongTien.ToString();
            //Đổ dữ liệu lên Drop
            for (int i = 1; i < 13; i++)
            {
                DropDownList2.Items.Add(i.ToString());
                
            }
        }

        protected void GV_DSHoaDon_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.Cells[5].Text == "0")
            //{
            //    e.Row.Cells[4].BackColor = Color.Yellow;
            //}
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<HoaDon> dsHoaDon = new List<HoaDon>();
            dsHoaDon = hd_Bus.LayDanhSach_HoaDon();
            int thang = int.Parse(DropDownList2.SelectedValue.ToString());
            List<HoaDon> dsHoaDon_Thang = new List<HoaDon>();
            dsHoaDon_Thang = dsHoaDon.Where(p => p.NgayDatMua.Month == thang).ToList();
            GV_DSHoaDon.DataSource = dsHoaDon_Thang;
            GV_DSHoaDon.DataBind();
            if (dsHoaDon_Thang.Count > 0)
            {
                for (int i = 0; i < dsHoaDon_Thang.Count - 1; i++)
                {
                    if (dsHoaDon_Thang[i].TinhTrang == 0)
                    {
                        GV_DSHoaDon.Rows[i].Cells[1].BackColor = Color.Goldenrod;
                        GV_DSHoaDon.Rows[i].Cells[2].BackColor = Color.Goldenrod;
                        GV_DSHoaDon.Rows[i].Cells[3].BackColor = Color.Goldenrod;
                        GV_DSHoaDon.Rows[i].Cells[4].BackColor = Color.Goldenrod;
                        GV_DSHoaDon.Rows[i].Cells[5].BackColor = Color.Goldenrod;
                    }
                }
                
                
            }
            double tongTien = 0;
            foreach (HoaDon hd in dsHoaDon_Thang)
            {
               tongTien = tongTien + hd.TongTien;
            }
            lb_tongDoanhThu.Text = tongTien.ToString();
        }

        protected void GV_DSHoaDon_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

        protected void GV_DSHoaDon_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            
        }

        protected void GV_DSHoaDon_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = int.Parse(((Label)GV_DSHoaDon.Rows[e.RowIndex].Cells[0].FindControl("lb_MaHoaDon")).Text);
            HoaDon hd = hd_Bus.LayThongTin_HoaDon(index);
            hd.TinhTrang = 1;
            hd_Bus.Sua_HoaDon(hd);
            GV_DSHoaDon.EditIndex = -1;
            Page_Load(sender,e);
        }

        protected void GV_DSHoaDon_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_DSHoaDon.EditIndex = e.NewEditIndex;
            Page_Load(sender, e);
        }
    }
}